package rank1400;

import java.util.HashSet;

/**
 * 2834. 找出美丽数组的最小和
 */
public class No2834 {
    /**
     * 1. 模拟（超时）
     */
    public int minimumPossibleSum(int n, int target) {
        int MOD = (int) (1e9 + 7);
        HashSet<Integer> set = new HashSet<>();
        set.add(1);
        int ans = 1, cur = 1;
        for (int i = 1; i < n; i++) {
            cur++;
            while (set.contains(target - cur)) cur++;
            set.add(cur);
            ans += cur;
            ans %= MOD;
        }
        return ans;
    }

    /**
     * 2. 数学
     */
    public int minimumPossibleSum2(int n, int target) {
        long m = Math.min(target / 2, n);
        return (int) ((m * (m + 1) + (n - m - 1 + target * 2) * (n - m)) / 2 % 1000000007);
    }
}
